<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Db_Profiler - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.db.profiler.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.db.profiler.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.db.statement.html">Zend_Db_Statement</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.db.html">Zend_Db</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.db.select.html">Zend_Db_Select</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.db.profiler" class="section"><div class="info"><h1 class="title">Zend_Db_Profiler</h1></div>
    

    <div class="section" id="zend.db.profiler.introduction"><div class="info"><h1 class="title">Introduction</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Db_Profiler</span> can be enabled to allow profiling of
            queries. Profiles include the queries processed by the adapter as
            well as elapsed time to run the queries, allowing inspection of the
            queries that have been performed without needing to add extra
            debugging code to classes. Advanced usage also allows the
            developer to filter which queries are profiled.
        </p>

        <p class="para">
            Enable the profiler by either passing a directive to the adapter
            constructor, or by asking the adapter to enable it later.
        </p>

        <pre class="programlisting brush: php">
$params = array(
    &#039;host&#039;     =&gt; &#039;127.0.0.1&#039;,
    &#039;username&#039; =&gt; &#039;webuser&#039;,
    &#039;password&#039; =&gt; &#039;xxxxxxxx&#039;,
    &#039;dbname&#039;   =&gt; &#039;test&#039;
    &#039;profiler&#039; =&gt; true  // turn on profiler
                        // set to false to disable (disabled by default)
);

$db = Zend_Db::factory(&#039;PDO_MYSQL&#039;, $params);

// turn off profiler:
$db-&gt;getProfiler()-&gt;setEnabled(false);

// turn on profiler:
$db-&gt;getProfiler()-&gt;setEnabled(true);
</pre>


        <p class="para">
            The value of the &#039;<span class="property">profiler</span>&#039; option is flexible. It is interpreted
            differently depending on its type. Most often, you should use a simple boolean value,
            but other types enable you to customize the profiler behavior.
        </p>

        <p class="para">
            A boolean argument sets the profiler to enabled if it is a <b><tt>TRUE</tt></b>
            value, or disabled if <b><tt>FALSE</tt></b>. The profiler class is the adapter&#039;s
            default profiler class, <span class="classname">Zend_Db_Profiler</span>.
        </p>

        <pre class="programlisting brush: php">
$params[&#039;profiler&#039;] = true;
$db = Zend_Db::factory(&#039;PDO_MYSQL&#039;, $params);
</pre>


        <p class="para">
            An instance of a profiler object makes the adapter use that object. The object type must
            be <span class="classname">Zend_Db_Profiler</span> or a subclass thereof. Enabling the profiler
            is done separately.
        </p>

        <pre class="programlisting brush: php">
$profiler = MyProject_Db_Profiler();
$profiler-&gt;setEnabled(true);
$params[&#039;profiler&#039;] = $profiler;
$db = Zend_Db::factory(&#039;PDO_MYSQL&#039;, $params);
</pre>


        <p class="para">
            The argument can be an associative array containing any or all of the keys
            &#039;<span class="property">enabled</span>&#039;, &#039;<span class="property">instance</span>&#039;, and
            &#039;<span class="property">class</span>&#039;. The &#039;<span class="property">enabled</span>&#039; and
            &#039;<span class="property">instance</span>&#039; keys correspond to the boolean and instance types
            documented above. The &#039;<span class="property">class</span>&#039; key is used to name a class to
            use for a custom profiler. The class must be <span class="classname">Zend_Db_Profiler</span> or
            a subclass. The class is instantiated with no constructor arguments. The
            &#039;<span class="property">class</span>&#039; option is ignored when the &#039;<span class="property">instance</span>&#039;
            option is supplied.
        </p>

        <pre class="programlisting brush: php">
$params[&#039;profiler&#039;] = array(
    &#039;enabled&#039; =&gt; true,
    &#039;class&#039;   =&gt; &#039;MyProject_Db_Profiler&#039;
);
$db = Zend_Db::factory(&#039;PDO_MYSQL&#039;, $params);
</pre>


        <p class="para">
            Finally, the argument can be an object of type <span class="classname">Zend_Config</span>
            containing properties, which are treated as the array keys described above. For example,
            a file &quot;<var class="filename">config.ini</var>&quot; might contain the following data:
        </p>

        <pre class="programlisting brush: ini">
[main]
db.profiler.class   = &quot;MyProject_Db_Profiler&quot;
db.profiler.enabled = true
</pre>


        <p class="para">
            This configuration can be applied by the following <acronym class="acronym">PHP</acronym> code:
        </p>

        <pre class="programlisting brush: php">
$config = new Zend_Config_Ini(&#039;config.ini&#039;, &#039;main&#039;);
$params[&#039;profiler&#039;] = $config-&gt;db-&gt;profiler;
$db = Zend_Db::factory(&#039;PDO_MYSQL&#039;, $params);
</pre>


        <p class="para">
            The &#039;<span class="property">instance</span>&#039; property may be used as in the following:
        </p>

        <pre class="programlisting brush: php">
$profiler = new MyProject_Db_Profiler();
$profiler-&gt;setEnabled(true);
$configData = array(
    &#039;instance&#039; =&gt; $profiler
    );
$config = new Zend_Config($configData);
$params[&#039;profiler&#039;] = $config;
$db = Zend_Db::factory(&#039;PDO_MYSQL&#039;, $params);
</pre>

    </div>

    <div class="section" id="zend.db.profiler.using"><div class="info"><h1 class="title">Using the Profiler</h1></div>
        

        <p class="para">
            At any point, grab the profiler using the adapter&#039;s
             <span class="methodname">getProfiler()</span> method:
        </p>

        <pre class="programlisting brush: php">
$profiler = $db-&gt;getProfiler();
</pre>


        <p class="para">
            This returns a <span class="classname">Zend_Db_Profiler</span> object instance. With
            that instance, the developer can examine your queries using a
            variety of methods:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                     <span class="methodname">getTotalNumQueries()</span> returns the total number
                    of queries that have been profiled.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getTotalElapsedSecs()</span> returns the total
                    number of seconds elapsed for all profiled queries.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getQueryProfiles()</span> returns an array of all
                    query profiles.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getLastQueryProfile()</span> returns the last (most
                    recent) query profile, regardless of whether or not the query
                    has finished (if it hasn&#039;t, the end time will be <b><tt>NULL</tt></b>)
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">clear()</span> clears any past query profiles
                    from the stack.
                </p>
            </li>
        </ul>

        <p class="para">
            The return value of  <span class="methodname">getLastQueryProfile()</span> and the
            individual elements of  <span class="methodname">getQueryProfiles()</span> are
            <span class="classname">Zend_Db_Profiler_Query</span> objects, which provide the
            ability to inspect the individual queries themselves:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                     <span class="methodname">getQuery()</span> returns the <acronym class="acronym">SQL</acronym> text of
                    the query. The <acronym class="acronym">SQL</acronym> text of a prepared statement with
                    parameters is the text at the time the query was prepared, so it contains
                    parameter placeholders, not the values used when the
                    statement is executed.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getQueryParams()</span> returns an array of
                    parameter values used when executing a prepared query.
                    This includes both bound parameters and arguments to the
                    statement&#039;s  <span class="methodname">execute()</span> method. The keys of
                    the array are the positional (1-based) or named (string)
                    parameter indices.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getElapsedSecs()</span> returns the number of
                    seconds the query ran.
                </p>
            </li>
        </ul>

        <p class="para">
            The information <span class="classname">Zend_Db_Profiler</span> provides is useful for
            profiling bottlenecks in applications, and for debugging queries
            that have been run. For instance, to see the exact query that was
            last run:
        </p>

        <pre class="programlisting brush: php">
$query = $profiler-&gt;getLastQueryProfile();

echo $query-&gt;getQuery();
</pre>


        <p class="para">
            Perhaps a page is generating slowly; use the profiler to determine
            first the total number of seconds of all queries, and then step
            through the queries to find the one that ran longest:
        </p>

        <pre class="programlisting brush: php">
$totalTime    = $profiler-&gt;getTotalElapsedSecs();
$queryCount   = $profiler-&gt;getTotalNumQueries();
$longestTime  = 0;
$longestQuery = null;

foreach ($profiler-&gt;getQueryProfiles() as $query) {
    if ($query-&gt;getElapsedSecs() &gt; $longestTime) {
        $longestTime  = $query-&gt;getElapsedSecs();
        $longestQuery = $query-&gt;getQuery();
    }
}

echo &#039;Executed &#039; . $queryCount . &#039; queries in &#039; . $totalTime .
     &#039; seconds&#039; . &quot;\n&quot;;
echo &#039;Average query length: &#039; . $totalTime / $queryCount .
     &#039; seconds&#039; . &quot;\n&quot;;
echo &#039;Queries per second: &#039; . $queryCount / $totalTime . &quot;\n&quot;;
echo &#039;Longest query length: &#039; . $longestTime . &quot;\n&quot;;
echo &quot;Longest query: \n&quot; . $longestQuery . &quot;\n&quot;;
</pre>

    </div>

    <div class="section" id="zend.db.profiler.advanced"><div class="info"><h1 class="title">Advanced Profiler Usage</h1></div>
        

        <p class="para">
            In addition to query inspection, the profiler also allows the
            developer to filter which queries get profiled. The following
            methods operate on a <span class="classname">Zend_Db_Profiler</span> instance:
        </p>

        <div class="section" id="zend.db.profiler.advanced.filtertime"><div class="info"><h1 class="title">Filter by query elapsed time</h1></div>
            

            <p class="para">
                 <span class="methodname">setFilterElapsedSecs()</span> allows the developer to set
                a minimum query time before a query is profiled. To remove the
                filter, pass the method a <b><tt>NULL</tt></b> value.
            </p>

            <pre class="programlisting brush: php">
// Only profile queries that take at least 5 seconds:
$profiler-&gt;setFilterElapsedSecs(5);

// Profile all queries regardless of length:
$profiler-&gt;setFilterElapsedSecs(null);
</pre>

        </div>

        <div class="section" id="zend.db.profiler.advanced.filtertype"><div class="info"><h1 class="title">Filter by query type</h1></div>
            

            <p class="para">
                 <span class="methodname">setFilterQueryType()</span> allows the developer to set
                which types of queries should be profiled; to profile multiple
                types, logical OR them. Query types are defined as the following
                <span class="classname">Zend_Db_Profiler</span> constants:
            </p>

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <b><tt>Zend_Db_Profiler::CONNECT</tt></b>: connection
                        operations, or selecting a database.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>Zend_Db_Profiler::QUERY</tt></b>: general database
                        queries that do not match other types.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>Zend_Db_Profiler::INSERT</tt></b>: any query that
                        adds new data to the database, generally <acronym class="acronym">SQL</acronym>
                        <acronym class="acronym">INSERT</acronym>.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>Zend_Db_Profiler::UPDATE</tt></b>: any query that
                        updates existing data, usually <acronym class="acronym">SQL</acronym>
                        <acronym class="acronym">UPDATE</acronym>.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>Zend_Db_Profiler::DELETE</tt></b>: any query that
                        deletes existing data, usually <acronym class="acronym">SQL</acronym>
                        <b><tt>DELETE</tt></b>.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>Zend_Db_Profiler::SELECT</tt></b>: any query that
                        retrieves existing data, usually <acronym class="acronym">SQL</acronym>
                        <acronym class="acronym">SELECT</acronym>.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>Zend_Db_Profiler::TRANSACTION</tt></b>: any
                        transactional operation, such as start transaction, commit,
                        or rollback.
                    </p>
                </li>
            </ul>

            <p class="para">
                As with  <span class="methodname">setFilterElapsedSecs()</span>, you can remove any
                existing filters by passing <b><tt>NULL</tt></b> as the sole
                argument.
            </p>

            <pre class="programlisting brush: php">
// profile only SELECT queries
$profiler-&gt;setFilterQueryType(Zend_Db_Profiler::SELECT);

// profile SELECT, INSERT, and UPDATE queries
$profiler-&gt;setFilterQueryType(Zend_Db_Profiler::SELECT |
                              Zend_Db_Profiler::INSERT |
                              Zend_Db_Profiler::UPDATE);

// profile DELETE queries
$profiler-&gt;setFilterQueryType(Zend_Db_Profiler::DELETE);

// Remove all filters
$profiler-&gt;setFilterQueryType(null);
</pre>

        </div>

        <div class="section" id="zend.db.profiler.advanced.getbytype"><div class="info"><h1 class="title">Retrieve profiles by query type</h1></div>
            

            <p class="para">
                Using  <span class="methodname">setFilterQueryType()</span> can cut down on the
                profiles generated. However, sometimes it can be more useful to
                keep all profiles, but view only those you need at a given
                moment. Another feature of  <span class="methodname">getQueryProfiles()</span> is
                that it can do this filtering on-the-fly, by passing a query
                type (or logical combination of query types) as its first
                argument; see <a href="zend.db.profiler.html#zend.db.profiler.advanced.filtertype" class="link">this
                    section</a> for a list of the query type constants.
            </p>

            <pre class="programlisting brush: php">
// Retrieve only SELECT query profiles
$profiles = $profiler-&gt;getQueryProfiles(Zend_Db_Profiler::SELECT);

// Retrieve only SELECT, INSERT, and UPDATE query profiles
$profiles = $profiler-&gt;getQueryProfiles(Zend_Db_Profiler::SELECT |
                                        Zend_Db_Profiler::INSERT |
                                        Zend_Db_Profiler::UPDATE);

// Retrieve DELETE query profiles
$profiles = $profiler-&gt;getQueryProfiles(Zend_Db_Profiler::DELETE);
</pre>

        </div>
    </div>

    <div class="section" id="zend.db.profiler.profilers"><div class="info"><h1 class="title">Specialized Profilers</h1></div>
        

        <p class="para">
            A Specialized Profiler is an object that inherits from
            <span class="classname">Zend_Db_Profiler</span>. Specialized Profilers treat
            profiling information in specific ways.
        </p>

        <div class="section" id="zend.db.profiler.profilers.firebug"><div class="info"><h1 class="title">Analisando o Desempenho com Firebug</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Db_Profiler_Firebug</span> envia informações de análise de
        desempenho para o
        <a href="http://www.getfirebug.com/" class="link external">&raquo; Firebug</a> <a href="http://getfirebug.com/logging.html" class="link external">&raquo; Console</a>.
    </p>

    <p class="para">
        Todos os dados são enviados via componente
        <span class="classname">Zend_Wildfire_Channel_HttpHeaders</span> que usa cabeçalhos
        <acronym class="acronym">HTTP</acronym> para assegurar que o conteúdo da página não fique
        confuso. É possivel depurar requisições <acronym class="acronym">AJAX</acronym> que exigem respostas
        <acronym class="acronym">JSON</acronym> e <acronym class="acronym">XML</acronym> com esta abordagem.
    </p>

    <p class="para">
        Requisitos:
    </p>

    <ul class="itemizedlist">
        <li class="listitem">
            <p class="para">
                Navegador Firefox preferencialmente na versão 3, mas a versão 2 também é suportada.
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                Firebug Firefox Extension que você pode baixar de <a href="https://addons.mozilla.org/en-US/firefox/addon/1843" class="link external">&raquo; https://addons.mozilla.org/en-US/firefox/addon/1843</a>.
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                FirePHP Firefox Extension que você pode baixar de <a href="https://addons.mozilla.org/en-US/firefox/addon/6149" class="link external">&raquo; https://addons.mozilla.org/en-US/firefox/addon/6149</a>.
            </p>
        </li>
    </ul>

    <div class="example" id="zend.db.profiler.profilers.firebug.example.with_front_controller"><div class="info"><p><b>Example #1 Análise de Desempenho de Banco de Dados com Zend_Controller_Front</b></p></div>
        

        <pre class="programlisting brush: php">
// Em seu arquivo de inicialização

$profiler = new Zend_Db_Profiler_Firebug(&#039;All DB Queries&#039;);
$profiler-&gt;setEnabled(true);

// Anexa o analisador de desempenho ao seu adaptador de banco de dados
$db-&gt;setProfiler($profiler);

// Despache seu controlador frontal

// Todas as consultas a banco de dados em seus arquivos de modelo,
// visão e controle serão agora analisadas e enviadas para o Firebug
</pre>

    </div>

    <div class="example" id="zend.db.profiler.profilers.firebug.example.without_front_controller"><div class="info"><p><b>Example #2 DB Profiling without Zend_Controller_Front</b></p></div>
        

        <pre class="programlisting brush: php">
$profiler = new Zend_Db_Profiler_Firebug(&#039;All DB Queries&#039;);
$profiler-&gt;setEnabled(true);

// Anexa o analisador de desempenho ao seu adaptador de banco de dados
$db-&gt;setProfiler($profiler);

$request  = new Zend_Controller_Request_Http();
$response = new Zend_Controller_Response_Http();
$channel  = Zend_Wildfire_Channel_HttpHeaders::getInstance();
$channel-&gt;setRequest($request);
$channel-&gt;setResponse($response);

// Inicia o buffering de saída
ob_start();

// Agora você pode rodar suas consultas de banco de dados a serem analisadas

// Libera a análise de desempenho para o navegador
$channel-&gt;flush();
$response-&gt;sendHeaders();
</pre>

    </div>
</div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.db.statement.html">Zend_Db_Statement</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.db.html">Zend_Db</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.db.select.html">Zend_Db_Select</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.db.html">Zend_Db</a></li>
  <li><a href="zend.db.adapter.html">Zend_Db_Adapter</a></li>
  <li><a href="zend.db.statement.html">Zend_Db_Statement</a></li>
  <li class="active"><a href="zend.db.profiler.html">Zend_Db_Profiler</a></li>
  <li><a href="zend.db.select.html">Zend_Db_Select</a></li>
  <li><a href="zend.db.table.html">Zend_Db_Table</a></li>
  <li><a href="zend.db.table.row.html">Zend_Db_Table_Row</a></li>
  <li><a href="zend.db.table.rowset.html">Zend_Db_Table_Rowset</a></li>
  <li><a href="zend.db.table.relationships.html">Zend_Db_Table Relationships</a></li>
  <li><a href="zend.db.table.definition.html">Zend_Db_Table_Definition</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>